package com.wxsoft.business.sys.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wxsoft.business.common.controller.BaseController;
import com.wxsoft.business.model.easyui.DataGrid;
import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.sys.pojo.SysRolesMenu;
import com.wxsoft.business.sys.pojo.SysUser;
import com.wxsoft.business.sys.service.ISysRolesMenuService;
import com.wxsoft.util.JsonUtil;
import com.wxsoft.util.UserUtil;

@Controller
@RequestMapping("/sysRolesMenu")
public class SysRolesMenuController extends BaseController {

	private Logger logger = Logger.getLogger(this.getClass());

	@Resource
	private ISysRolesMenuService service;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "sys/sysRolesMenulist";
	}

	@RequestMapping("/datagrid")
	public void list(HttpServletRequest request, PageHelper page,
			SysRolesMenu sysRolesMenu, HttpServletResponse response)
			throws IOException {

		DataGrid dg = new DataGrid();
		dg.setTotal(service.findCount(sysRolesMenu));
		List<SysRolesMenu> sysRolesMenuList = service.findAll(page,
				sysRolesMenu);
		dg.setRows(sysRolesMenuList);
		JsonUtil.toResponse(response, dg);

	}

//	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void add(HttpServletRequest request, HttpServletResponse response,
			SysRolesMenu sysRolesMenu) {
		try {
			SysUser user = UserUtil.getUser(request);
			sysRolesMenu.setHandle(user.getUser_id());
			sysRolesMenu.setHandle_date(new Date());
			String r = service.add(sysRolesMenu);
			this.writeReturn(request, response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request, response, e.getMessage());
		}
	}

//	@ResponseBody
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public void modify(HttpServletRequest request,
			HttpServletResponse response, SysRolesMenu sysRolesMenu) {
		try {
			SysUser user = UserUtil.getUser(request);
			sysRolesMenu.setHandle(user.getUser_id());
			sysRolesMenu.setHandle_date(new Date());
			String r = service.modify(sysRolesMenu);
			this.writeReturn(request, response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request, response, e.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request,
			HttpServletResponse response, SysRolesMenu sysRolesMenu) {
		try {
			String r = service.delete(sysRolesMenu);
			this.writeReturn(request, response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request, response, e.getMessage());
		}
	}

//	@ResponseBody
	@RequestMapping(value = "/addForRole", method = RequestMethod.POST)
	public void addForRole(HttpServletRequest request,
			HttpServletResponse response, SysRolesMenu sysRolesMenu,
			String menuIds) {
		try {
			System.out.println(menuIds);
			SysUser user = UserUtil.getUser(request);
			String r = service.addForRole(sysRolesMenu,user,menuIds);
			this.writeReturn(request, response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request, response, false, e.getMessage());
		}
	}
}
